package com.wd.tech.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.BaseRequestOptions;
import com.bumptech.glide.request.RequestOptions;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wd.tech.R;
import com.wd.tech.bean.PlateBean;
import com.wd.tech.view.activity.PlateActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * date:2020/4/23
 * author:郑惠洋(Dell)
 * function:所有模块 Adapter
 */
public class PlateAdapter extends XRecyclerView.Adapter<PlateAdapter.VH> {



    private Context context;
    private List<PlateBean.ResultBean> list;

    public PlateAdapter(Context context, ArrayList<PlateBean.ResultBean> list) {
        this.context = context;
        this.list = list;
    }



    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_palet, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        PlateBean.ResultBean resultBean = list.get(position);
        holder.tvName.setText(resultBean.getName());

        String pic = resultBean.getPic();
        Glide.with(context)
                .load(pic)
                .error(R.mipmap.ic_launcher)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(5)))
                .into(holder.ivPlate);

        //接口回调根据id
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick!=null){
                    onClick.setonItemClick(list.get(position).getName(),list.get(position).getId());
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class VH extends XRecyclerView.ViewHolder {
        @BindView(R.id.iv_plate)
        ImageView ivPlate;
        @BindView(R.id.tv_name)
        TextView tvName;

        public VH(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //列表自定义回调接口
    public interface onItemClick{
        void setonItemClick(String techName,int id);
    }

    private onItemClick onClick;

    public void setOnClick(onItemClick onClick){
        this.onClick=onClick;
    }



}
